Skip to content

Add threadsafe dynamic direct peer handling to GossipSub#673

Open
cortze wants to merge 7 commits intolibp2p:masterfrom
cortze:allow-dynamic-direct-peers
Open

Add threadsafe dynamic direct peer handling to GossipSub#673
cortze wants to merge 7 commits intolibp2p:masterfrom
cortze:allow-dynamic-direct-peers

Conversation

@cortze
Copy link
Contributor

@cortze cortze commented Feb 13, 2026

Description

The current direct peer handling is a bit clunky, it only allows direct peers to be set at the start of the service. This makes the application layer unable to modify the direct peer list, or to create new services as we want to add/remove peers to that list.

The PR adds two new self-descriptive methods to the GossipSubRouter struct:

  • AddDirectPeer()
  • RemoveDirectPeer()
    Both handling the underlaying Peerstore and TagTracer changes as well. Ensuring that we have all in place to ensure the connection protected with the remote peer.

remove race condition on direct peering test

rm missing race-condition
@cortze cortze force-pushed the allow-dynamic-direct-peers branch from 943b2e8 to c24fe95 Compare February 17, 2026 14:48
@MarcoPolo
Copy link
Contributor

I pushed the latest commit @cortze, please take a look

@cortze
Copy link
Contributor Author

cortze commented Feb 18, 2026

I just fixed the test and covered a possible nil pointer given to the tag_tracer when the direct peer list was empty at the GossipSubRouter startup.

This should be ready to merge, but let me know if I should address anything else ✌🏽

@cortze cortze requested a review from MarcoPolo February 19, 2026 17:08
tag_tracer.go Outdated
t.idGen = gs.p.idGen
t.direct = gs.direct
if gs.direct != nil {
t.direct = gs.direct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this rely on having a stable pointer to t.direct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it intentional that this aliases to the gossipsub map?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that it aliases to the gossipsub map. It means that if gs does not initialize the map this will be a nil pointer that will be dereferenced later. I've changed it to use a function instead.

tag_tracer.go Outdated
func (t *tagTracer) addDirectPeer(p peer.ID) {
t.Lock()
defer t.Unlock()
t.direct[p] = struct{}{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do this if this is aliased to the gossipsub map?

tag_tracer.go Outdated
}

func (t *tagTracer) addDirectPeer(p peer.ID) {
t.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the lock here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments